package com.sn.system.common.utils;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import com.sn.system.common.constants.Fomart;


public final class ReflectionHelper {
    /**
     * format方法のキャッシュ
     */
    public static Map formatMethodMap;
    
    public static Map<String,Class> classMap = new HashMap<String, Class>();

//    static {
//    	ini();
//    }
    
    public static void ini(){
    	try {
    		formatMethodMap=new HashMap();
    		Class c = Class.forName("jp.co.sn.system.common.constants.Fomart");
    		Fomart fomart = (Fomart)c.newInstance();
			
		//	formatMethodMap.put("HTM", c.getMethod("HTM", Object.class,Object.class,Object.class,Object.class,Object.class,Object.class,Object.class,Object.class,Object.class,Object.class,Object.class,Object.class,Object.class,Object.class,Object.class));
			} catch (SecurityException e) {
			e.printStackTrace();
//		} catch (NoSuchMethodException e) {
//			e.printStackTrace();
//			System.out.println(e.toString());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
    }

    /**
     * format方法の実施
     */
    public static String doFormat(String format,Object... data){
		Object result=data;
		Fomart fomart=(Fomart)formatMethodMap.get("Fomart");
		Method m=(Method)formatMethodMap.get(format);
		if(m!=null){
			try {
				result=m.invoke(fomart,data);
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
		}else{
			System.out.println("Method: "+format+"is not Exist");
		}
		return result.toString();
    }

    public static <T> T getReflectObj(Class<T> entityClass){
    	Object bean=null;
		try {
			bean = Class.forName(entityClass.getName().toString()).newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
    	return (T)bean;
    }
    
    public static Class  getReflectClass(String className){
    	if(classMap.containsKey(className)){
    		return classMap.get(className);
    	}

		try {
			Class c = Class.forName(className);
			return c;
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
    	return null;
    }
    
}
